#!/usr/bin/env python

import os

from hg   import Hg
from git  import Git
#from mantux_data.meta import Meta


FOLDER = ".mantux"

def cmd_list ( self, args ):
	res = {}

	for el in self.repos.values ():
		if el.get ( "hidden" ): continue
		res [ el [ 'name' ] ] = el #print el [ 'name' ],'-', el [ 'desc' ]

	keys = res.keys ()
	keys.sort ()
	for k in keys:
		el = res [ k ]
		print el [ 'name' ],'-', el [ 'desc' ]
		

def cmd_search ( self, args ):
	base_str = args.get ( 'base_str' )
	if not base_str: return

	for k in self.repos.keys ():
		if k.find ( base_str ) != -1:
			print k, self.repos [ k ] [ 'desc' ]

def cmd_install ( self, args ):
	repos = args.get ( 'repos' )

	if not repos: return

	self._deps_load ()

	for k in repos:
		_install ( self, k )

	self._deps_save ()

def cmd_help ( self, args ):
	if not args: 
		print "ERROR: specify command"
		return

	cmd = args [ 'command' ] [ 0 ]
	m = self.commands [ cmd ]

	print "HELP for '%s':" % cmd, m.get ( "help", "No help available" )

def cmd_show_cfg ( self, args ):
	for k, v in self.cfg.iteritems ():
		print "%s: %s" % ( k, v )

def cmd_init ( self, args ):
	if not os.path.exists ( FOLDER ): os.mkdir ( FOLDER )

	self._write_if_missing ( os.path.join ( FOLDER, "config" ), "{}" )
	self._write_if_missing ( os.path.join ( FOLDER, "deps" ), "{}" )

def cmd_reinstall ( self, args ):
	self._deps_load ()
	os.chdir (self._find_project_root () )

	for k, v in self.deps.iteritems ():
		_install ( self, k, True )

def _install ( self, name, force = False ):
	base_dir = os.getcwd ()

	if name not in self.repos:
		print "ERROR: could not find module: %s" % name
		return

	if not force and name in self.deps: return

	k = self.repos [ name ]

	for dep in k.get ( "deps", [] ):
		_install ( self, dep, force )

	install_dir = self._go_to_install_dir ( k )
	version = -1
	vcs = None


	if k [ 'vcs' ] == 'hg':
		vcs = Hg ( k [ 'repo' ] )
	elif k [ 'vcs' ] == 'git':
		vcs = Git ( k [ 'repo' ] )
	elif k [ 'vcs' ] == 'META':
		version = -1

	if vcs and not self.exclude ( dirname = os.path.join ( install_dir, vcs.dirname () ) ):
		print "INSTALL: %s" % name
		if vcs:
			vcs.clone ()
			version = vcs.version ()

		if version != -1:
			self._deps_add ( k, version, install_dir, vcs.dirname () )

	os.chdir ( base_dir )


def install_commands ( mantux ):
	cmds = [
		{ "name" : "list",      "func" : cmd_list,      "params" : "" },
		{ "name" : "search",    "func" : cmd_search,    "params" : [ ( 'base_str', dict ( help = 'List of repos to be searched' ) ) ], "help" : "Searches all the repos for the given pattern" },
		{ "name" : "install",   "func" : cmd_install,   "params" : [ ( 'repos', dict ( nargs = '+', help = 'List of repos to be installed' ) ) ], "help" : "Installs one or more repo" },
		{ "name" : "init",      "func" : cmd_init,      "params" : "" },
		{ "name" : "help",      "func" : cmd_help,      "params" : [ ( 'command', dict ( nargs = 1, help = "Command you want the help on" ) ) ] },
		{ "name" : "show_cfg",  "func" : cmd_show_cfg,  "params" : "" },
		{ "name" : "reinstall", "func" : cmd_reinstall, "params" : "" },

	]

	for c in cmds:
		mantux.commands [ c [ 'name' ] ] = c
